package com.hujf.netty.example.client;

import com.hujf.netty.example.handle.SimpleHandlerForClient;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author Hujf
 * @title: SimpleClient
 * @date 2021/1/28 0028下午 2:07
 * @description: TODO
 */
public class SimpleClient {
    public static void main(String[] args) throws InterruptedException {
        //客户端只需要一个
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        try {
            //创建启动对象
            Bootstrap client = new Bootstrap();
            //相关参数设置
            client.group(eventLoopGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new SimpleHandlerForClient());
                        }
                    });
            ChannelFuture cf = client.connect("localhost", 8889).sync();
            cf.channel().closeFuture().sync();
        } finally {
            eventLoopGroup.shutdownGracefully();
        }

    }
}
